ShowTable of Contents
To create an email that contains a HTML body is a quite simple process, but there is a lot to
think about because of the variety of email-clients and their specific behaviour in how they implement
the HTML/CSS specifications. I will here try to guide You through the complete steps needed to
accomplish a mail that renders correctly in the recipients mailclient.
Step-by-step
We will fist declare the necessary variables...
Dim session As New NotesSession
Dim db As NotesDatabase
Dim doc As NotesDocument
Dim mimeRoot As NotesMIMEEntity
Dim mime As NotesMIMEEntity
Dim header As NotesMIMEHeader
Dim stream As NotesStream
Dim sHtml As String
As You will notice - we will be working with MIME enteties to create the mail, both the body and the mail header.
When working with the NotesMIMEEntety object, we must turn of the MIME conversion that is turned on by default.
This is extremely important, because if You forget to do this, You can experience serious troubles!
session.ConvertMime = False
Now we're ready to start creating the mail document:
Set db = session.CurrentDatabase
Set doc = db.CreateDocument
doc.Form = "Memo"
doc.Principal = "System"
You can change the Principal field to any text You want to display as the sender. Next step is to create the headers:
Set mimeRoot = doc.Createmimeentity
Set mime = mimeRoot.Createchildentity
Set header = mimeRoot.Getnthheader("Content-Type")
Call header.SetHeaderval("multipart/related")
doc.Subject = "Test of HTML Mail"
Set header = mime.CreateHeader("Subject")
Call header.Setheaderval("Test of HTML Mail")
Why defining the subject twice (both in the Subject field and as a MIME header value)?
Well, that is because the recipient(s) client(s) can choose to NOT display HTML and only show the
text part of the mail - and therefore You need to define the field as well. It is the field that will become
the text representation of the subject.
Now, lets set the recipients address(es) and the "From" address:
It is finally time to define the HTML body. Let's assign all the HTML code into a string variable:
sHtml = |
<style>
body {font-family:Verdana,Arial,sans-serif;
color:black;
font-weight:normal;
font-size:10px;
}
h3 { font-family:Verdana,Arial,sans-serif;font-size:17px; font-weight:normal; color:black;}
table,tr,td {font-family:Verdana,Arial,sans-serif;font-size:11px;font-weight:normal; color:black;}
th {font-family:Verdana,Arial,sans-serif;font-size:11px;font-weight:bold;color:black;}
</style>
<table border="0" cellspacing="0" cellpadding="0">
<tr>
<td style="font-family:Verdana,Arial,sans-serif;font-size:11px;font-weight:normal;color:black;
background-repeat:no-repeat;background-position:right bottom;" background="cid:leaf.gif">
<div style="font-size:11px; padding-left:25px;padding-right:10px;padding-top:8px;width:630px;
border:2px solid #0087A9;">
<img src="cid:logotype.jpg" border="0" alt="D-Link Logo" align="left" height="32" width="147" />
<br /><br /><br /><br />
<p style="font-family:Verdana,Arial,sans-serif;font-size:11px;padding-left:15px; padding-right:10px;color:black;">
<h1 style="font-family:Verdana,Arial,sans-serif;font-size:17px; font-weight:normal; color:black;">
** This is an automated message, please do not reply **</h1>
<p>Lorem ipsum dolor sit amet, consectetur adipiscing elit. Curabitur dapibus vehicula sapien
in vulputate. Nullam rhoncus dolor at erat faucibus scelerisque. Donec a tristique purus. Class aptent
taciti sociosqu ad litora torquent per conubia nostra, per inceptos himenaeos. Donec eget urna sit amet
ligula aliquet lacinia. Praesent fringilla elementum quam et vulputate. Nam bibendum, sapien sed
adipiscing sodales, augue neque euismod tortor, eget ornare tortor justo id tellus. Fusce id lorem
at nulla laoreet eleifend. Fusce odio diam, aliquam in semper sit amet, condimentum eget odio.
Praesent dapibus urna condimentum mi rhoncus venenatis. Morbi ullamcorper libero in ligula tempor
hendrerit. Vivamus pretium pharetra malesuada. Vestibulum cursus turpis eget risus ultricies faucibus
eu eget urna. Donec aliquet convallis pulvinar.
</p>
<br />
<br />
</div>
</td></tr></table>|
Did You notice something odd with the HTML code? How about the imgage source and the background source?
It said 'cid:leaf.gif' and 'cid:logotype.jpg'. Well, it is a marker for the MIME object that it should insert a picture from
another MIME object inside the same message. Therefore we will need to attach the images as well into MIME
objects and here comes a really bad thing when attaching files into a Notesdocument: You must have access to
the file system and therefore You need to think about a couple of things:
1) Is the agent going to be run on the server or locally?
If You run it only locally - then the picture files must reside on the local computer that runs the agent.
If You run it scheduled on a server - then the picture must reside on the servers filesystem and here comes another
thing: You must therefore also set the agent security properties to allow restricted operations.
So, time to attach the HTML body:
Set stream = session.CreateStream
Call stream.WriteText(sHtml)
Call mime.SetContentFromText(stream,"text/html",ENC_NONE)
Call stream.Close
We now attach the images:
' Create Inline image reference:
Set mime = mimeRoot.Createchildentity
Call stream.Open("C:\Temp\leaf.gif")
Call mime.SetContentFromBytes(stream,"image/gif",ENC_NONE)
Call stream.Close
Call mime.EncodeContent(ENC_BASE64)
Set header = mime.CreateHeader("Content-ID")
Call header.SetHeaderVal("<leaf.gif>")
' Next picture:
Set mime = mimeRoot.Createchildentity
Call stream.Open("C:\Temp\logotype.jpg")
Call mime.SetContentFromBytes(stream,"image/jpg",ENC_NONE)
Call stream.Close
Call mime.EncodeContent(ENC_BASE64)
Set header = mime.CreateHeader("Content-ID")
Call header.SetHeaderVal("<logotype.jpg>")
Now, if the mail is like a newsletter where You don't want do display the recipients names, then we do like this:
Finally - time to send the mail! Oh - and don't forget to turn on the MIME conversion at the very end of the agent!
Call doc.Send(False)
session.ConvertMime = True
Complete code listing
Here is the complete code as described above:
Dim session As New NotesSession
Dim db As NotesDatabase
Dim doc As NotesDocument
Dim mimeRoot As NotesMIMEEntity
Dim mime As NotesMIMEEntity
Dim header As NotesMIMEHeader
Dim stream As NotesStream
Dim sHtml As String
session.ConvertMime = False
Set db = session.CurrentDatabase
Set doc = db.CreateDocument
doc.Form = "Memo"
doc.Principal = "System"
Set mimeRoot = doc.Createmimeentity
Set mime = mimeRoot.Createchildentity
Set header = mimeRoot.Getnthheader("Content-Type")
Call header.SetHeaderval("multipart/related")
doc.Subject = "Test of HTML Mail"
Set header = mime.CreateHeader("Subject")
Call header.Setheaderval("Test of HTML Mail")
Set header = mime.CreateHeader("To")
Call header.SetHeaderval("kenneth.axi@lycos.com")
Set header = mime.CreateHeader("From")
Call header.setheaderval("no-reply@lycos.com")
sHtml = |
<style>
body {font-family:Verdana,Arial,sans-serif;
color:black;
font-weight:normal;
font-size:10px;
}
h3 { font-family:Verdana,Arial,sans-serif;font-size:17px; font-weight:normal; color:black;}
table,tr,td {font-family:Verdana,Arial,sans-serif;font-size:11px;font-weight:normal; color:black;}
th {font-family:Verdana,Arial,sans-serif;font-size:11px;font-weight:bold;color:black;}
</style>
<table border="0" cellspacing="0" cellpadding="0">
<tr><td style="font-family:Verdana,Arial,sans-serif;font-size:11px;font-weight:normal;color:black;
background-repeat:no-repeat;background-position:right bottom;" background="cid:leaf.gif">
<div style="font-size:11px; padding-left:25px;padding-right:10px;padding-top:8px;width:630px;
border:2px solid #0087A9;">
<img src="cid:logotype.jpg" border="0" alt="D-Link Logo" align="left" height="32" width="147" />
<br /><br /><br /><br />
<p style="font-family:Verdana,Arial,sans-serif;font-size:11px;padding-left:15px; padding-right:10px;color:black;">
<h1 style="font-family:Verdana,Arial,sans-serif;font-size:17px; font-weight:normal; color:black;">
** This is an automated message, please do not reply **</h1>
<p>Lorem ipsum dolor sit amet, consectetur adipiscing elit. Curabitur dapibus vehicula sapien in vulputate.
Nullam rhoncus dolor at erat faucibus scelerisque. Donec a tristique purus. Class aptent taciti sociosqu ad
litora torquent per conubia nostra, per inceptos himenaeos. Donec eget urna sit amet ligula aliquet lacinia.
Praesent fringilla elementum quam et vulputate. Nam bibendum, sapien sed adipiscing sodales, augue
neque euismod tortor, eget ornare tortor justo id tellus. Fusce id lorem at nulla laoreet eleifend. Fusce odio
diam, aliquam in semper sit amet, condimentum eget odio. Praesent dapibus urna condimentum mi
rhoncus venenatis. Morbi ullamcorper libero in ligula tempor hendrerit. Vivamus pretium pharetra malesuada.
Vestibulum cursus turpis eget risus ultricies faucibus eu eget urna. Donec aliquet convallis pulvinar.
</p>
<br />
<br />
</div>
</td></tr></table>|
Set stream = session.CreateStream
Call stream.WriteText(sHtml)
Call mime.SetContentFromText(stream,"text/html",ENC_NONE)
Call stream.Close
' Create Inline image reference:
Set mime = mimeRoot.Createchildentity
Call stream.Open("C:\Temp\leaf.gif")
Call mime.SetContentFromBytes(stream,"image/gif",ENC_NONE)
Call stream.Close
Call mime.EncodeContent(ENC_BASE64)
Set header = mime.CreateHeader("Content-ID")
Call header.SetHeaderVal("<leaf.gif>")
' Next picture:
Set mime = mimeRoot.Createchildentity
Call stream.Open("C:\Temp\logotype.jpg")
Call mime.SetContentFromBytes(stream,"image/jpg",ENC_NONE)
Call stream.Close
Call mime.EncodeContent(ENC_BASE64)
Set header = mime.CreateHeader("Content-ID")
Call header.SetHeaderVal("<logotype.jpg>")
Dim rec(2) As String
rec(0) = "kenneth.axi@lycos.com"
rec(1) = "kenneth.axi@synergica.com"
rec(2) = "abc@def.com"
Dim itm As NotesItem
Set itm = New NotesItem(doc,"Recipients","no-reply@lycos.com",NAMES)
doc.SendTo = "no-reply@lycos.com"
doc.BlindCopyTo = rec
Call doc.Send(False)
session.ConvertMime = True
More information: Differences in mail clients implementation of HTML and CSS standards
Now, a couple of last warnings... As I mentioned earlier, there are differences in how the mail clients have
implemented the HTML and CSS standards. For instance, Gmail does not allow the style-tag to be defined
at ALL and will therefore be removed before rendering the message. That is why we need to use the style
property of each HTML tag. To learn more about the differences,
take a look at Guide to CSS support in email clients.
One thing about the list though: It is incorrect when it states that Lotus Notes 8.5 doesn't support the background
property in CSS. I have successfully used this property many times with Lotus Notes clients. The same goes
with "background-position".